Skip to main content

userdel — Secure Account Offboarding

Learning Focus

By the end of this lesson, you will be able to remove inactive users safely, handle running processes, decide when to keep or remove home directories, and reassign WordPress ownership after account deletion.

Overview

userdel removes user accounts from Linux identity databases. It is the final step of account lifecycle management after suspension and review.

On WordPress VPS systems, careless deletion can leave orphaned files or permission issues. A safe workflow ensures user removal does not break deploy pipelines or web write access.

Tool Snapshot
  • Core Function: Delete local Linux user accounts and optionally their home/mail data.
  • Primary Benefit: Reduces attack surface by removing stale identities.
  • Where to Use: Contractor offboarding, incident containment, account cleanup cycles.
  • Workflow: userdel [OPTIONS] USERNAME.

userdel is provided by shadow utilities and updates /etc/passwd, /etc/shadow, and /etc/group.

System Check

Ensure userdel is available and check your version:

which userdel # Expected: /usr/sbin/userdel
userdel --help # Shows supported options

Syntax & Expression Rules

The command follows a logical structure that reads almost like a sentence:

userdel [OPTIONS] USERNAME
  • [OPTIONS]: Deletion mode flags such as -r and -f.
  • USERNAME: Existing account to remove.
  • (pre-checks): Verify running processes and owned files before deletion.

Deletion Flags

ExpressionDescriptionExample Syntax⭐ Rating
:--:--:--:--
(no flag)Delete account entry onlysudo userdel wpdev⭐⭐⭐⭐
-rDelete account plus home directory and mail spoolsudo userdel -r wpdev⭐⭐⭐⭐⭐
-fForce deletion even with active processessudo userdel -f compromised1⭐⭐⭐
-ZRemove SELinux mapping (if supported)sudo userdel -Z appuser
--helpShow syntax and available optionsuserdel --help⭐⭐

Offboarding Actions

ActionDescriptionWordPress/VPS Use CaseExample Syntax
:--:--:--:--
Pre-delete process checkConfirm user is not actively running jobsSafe offboarding without service interruptionps -u wpdev
Kill active sessionsStop lingering user processesEmergency removal after suspicious activitysudo pkill -u wpdev
Remove account + homeComplete cleanup for departed userContractor account retirementsudo userdel -r contractor1
Restore web ownershipCorrect file permissions after user deletionKeep WordPress writable by web stacksudo chown -R www-data:www-data /var/www/html

Practical Use Cases

1. Delete account but keep home directory for archival

sudo userdel wpdev

Expected output:

# (no output on success)

Explanation: Removes account identity while preserving /home/wpdev. Use case: Offboarding where legal/data retention requires user files.

2. Delete account and home directory completely

sudo userdel -r contractor1

Expected output:

userdel: contractor1 mail spool (/var/mail/contractor1) not found

Explanation: Fully removes user identity and home data. Use case: Standard contractor cleanup.

3. Handle active sessions before deletion

ps -u wpdev && sudo pkill -u wpdev && sudo userdel -r wpdev

Expected output:

PID TTY TIME CMD
11234 pts/2 00:00:00 bash

Explanation: Stops user processes, then removes account. Use case: Prevent deletion failures due to active processes.

4. Force-delete compromised account rapidly

sudo userdel -f compromised1

Expected output:

# (no output on success)

Explanation: Deletes user entry even if process/session checks are bypassed. Use case: Emergency containment workflow.

5. Verify account no longer exists

getent passwd wpdev || echo "removed"

Expected output:

removed

Explanation: Confirms identity record is gone. Use case: Post-offboarding verification.

6. Find files still owned by deleted UID

sudo find /var/www -uid 1012 -ls

Expected output:

245793 4 -rw-r--r-- 1 1012 33 1200 Feb 22 18:41 /var/www/html/wp-content/uploads/legacy.txt

Explanation: Detects orphaned ownership after account deletion. Use case: Prevent subtle permission failures.

7. Reassign WordPress ownership after offboarding

sudo chown -R www-data:www-data /var/www/html

Expected output:

# (no output on success)

Explanation: Restores expected owner/group across site files. Use case: Maintain plugin upload/update functionality.

8. Record deletion in audit log

echo "$(date -Iseconds) deleted user contractor1" | sudo tee -a /var/log/user_audit.log

Expected output:

2026-02-23T10:15:30+00:00 deleted user contractor1

Explanation: Leaves explicit offboarding trace. Use case: Compliance and incident documentation.

Common Mistakes & Troubleshooting

ProblemCauseFix
:--:--:--
userdel: user is currently used by processTarget user still has active processesEnd sessions first: sudo pkill -u USER && sudo userdel -r USER
Home directory remains after deletion-r flag omittedRemove manually after review: sudo rm -rf /home/USER
WordPress updates fail after offboardingFiles still owned by removed UIDReassign ownership: sudo chown -R www-data:www-data /var/www/html
Account appears to still exist in scriptsCache or typo in validationVerify with getent passwd USER and id USER
Critical data lost during deletionUsed -r without backupRestore from backup; adopt pre-delete archive step with tar

Best Practices

  • Always pre-check process activity: Prevent partial deletion and service disruption.
  • Choose deletion mode deliberately: Keep home for retention cases; use -r for full cleanup.
  • Back up before destructive steps: Archive user home if business or legal retention applies.
  • Reassign production ownership immediately: Keep /var/www/html mapped to www-data.
  • Log every offboarding action: Maintain accountability and rollback context.

Hands-On Practice

Task: Offboard a Temporary WordPress Contractor Cleanly

  1. Inspect sessions with ps -u contractor1; stop them using sudo pkill -u contractor1.
  2. Remove account and home with sudo userdel -r contractor1; confirm with getent passwd contractor1.
  3. Challenge: Scan /var/www for orphaned UIDs and normalize ownership back to www-data:www-data.

Connection to Other Concepts

  • passwd: Lock accounts first during urgent containment before final deletion.
  • usermod: Alternative when you need to disable/restrict instead of delete.
  • id: Verify identity and UID mapping before and after offboarding.
  • who: Detect active sessions that must be terminated before deletion.

Visual Learning Diagram

What's Next: Proceed to who — Monitor Active Sessions in Real Time to improve visibility before and during account changes.